<?php

namespace app\api\model\group;

use app\logic\model\group\GroupOrderLogicModel;

class GroupOrderApiModel extends GroupOrderLogicModel
{

    public function getFindByUserIdAndId(int $id = 0, int $user_id = 0,$field='*')
    {
        if(empty($id) || empty($user_id)){
            return false;
        }
        return $this->where('id',$id)
            ->where('buyer_id',$user_id)
            ->field($field)
            ->find();
    }
    public function getApiList(array $param,$where=[],$filed="*",$page=1,$limit=15,$order="id desc"){
        $query = $this->where($where)
            ->field($filed)
            ->where(function($q) use ($param){
                if(check_request($param,'status')){
                    $q->where('group_order_api_model.status',$param['status']);
                }
                if(check_request($param,'buyer_id')){
                    $q->where('buyer_id',$param['buyer_id']);
                }
            })
            ->withJoin([
                'goods'=>['id','title','img','goods_id','type','end_time','num','complete_num'],
            ]);
        $count = $query->count();
        $list = $query->limit($limit)
            ->select();
        if($count >=1 ){
            foreach ($list as &$val){
                $val['goods']['num'] = GroupOrderLogicModel::getApiBuyNum($val['goods']);
            }
            unset($val);
        }
        return [$list,$count];
    }

    public function details($id, string $field='*')
    {
        $info = $this->where('id',$id)
            ->field($field)
            ->find();

    }
}